var LOGISTICS = 1,
    TRADE = 2,
    LOGISTICS_TRADE = 3,
    SUPPLER_TRADE = 256;
var removeMenuItem = {}
/*data为空数组表示全部删除 title为空表示不删除 data不为空只删除子项*/
removeMenuItem["primary"] = [{
    "title": "个人账号",
    "data": [{
        "name": "子账号管理"
    }]
}];
removeMenuItem[LOGISTICS] = [{
    title: "贸易",
    data: []
}]
removeMenuItem[TRADE] = [{
    title: "物流",
    data: []
}]
removeMenuItem[SUPPLER_TRADE] = [{
    title: "物流",
    data: []
},{
    title: "贸易",
    data: [{
        title: "订单",
        data: [{
            name: "直接下单"
        }]
    }]
}
]
var sliderComponent = React.createClass({
    tree: [],
    pageUrl: "",
    userType: "",
    componentDidMount: function() {
        easyMe.useJs(["javascripts/widget/slider/sliderView.js"]);
        var _this = this;
        if(cookie.get('primary') == 'false'){
            $('a[href="/sub_bill"]').parent('li').remove();
        }
        relateUrl = easyMe.getQueryString("relate");
        if(relateUrl) {
            this.pageUrl = "/" + relateUrl;
        }
        sliderView.init(this.state.data.data, this.pageUrl, this.userType);
        sliderView.bindEvent();
        //sliderView.openAllSlider();
        sliderView.openCurrentSlider();//将当前选中的树展开
    },
    receiveMsg: function(data) {

    },
    getInitialState: function () {
        return this.props ? this.props : {};
    },
    getMenu: function(slider) {
        for(var index = 0; index <  slider.length; index++) {
            if(slider[index].title) {
                if(slider[index].title === "账户"){
                    this.tree.push(<li key={"key"+index} className="item-title" data-index={index}><div className="menu-title"><em className="account"></em>{slider[index].title}</div></li>);
                }else if(slider[index].title === "个人账号"){
                    this.tree.push(<li key={"key"+index} className="item-title" data-index={index}><div className="menu-title"><em className="personal-account"></em>{slider[index].title}</div></li>);
                }else if(slider[index].title === "物流"){
                    this.tree.push(<li key={"key"+index} className="item-title" data-index={index}><div className="menu-title"><em className="logistics"></em>{slider[index].title}</div></li>);
                }else if(slider[index].title === "贸易"){
                    this.tree.push(<li key={"key"+index} className="item-title" data-index={index}><div className="menu-title"><em className="trade"></em>{slider[index].title}</div></li>);
                }else{
                    this.tree.push(<li key={"key"+index} className="item-title" data-index={index}><div className="menu-title">{slider[index].title}</div></li>);
                }
            } else {
                var item;
                if(slider[index].data) {
                    item = slider[index].data;
                } else {
                    item = slider[index];
                }
                this.tree.push(<li key={item.id} className={(this.pageUrl.indexOf(item.url) > -1 || this.pageUrl.indexOf(item.relate) > -1) ? "active" : ""}><div className="border-active"></div><a href={item.url}>{item.name}</a></li>);
            }
        }
        return this.tree;
    },
    removeSliderMenuItem: function(slider, title, name, isTrue) {
        if(!slider) {
            return;
        }
        for(var index = 0; index < slider.length; index++) {
            if(slider[index].title == title || isTrue) {
                if(slider[index].data && slider[index].data.length) {
                    this.removeSliderMenuItem(slider[index].data, title, name, true);
                } else {
                    if(slider[index].name == name) {
                        slider.splice(index--, 1);
                    }
                }
            }
        }
        return slider;
    },
    removeChildMenu: function(slider, removeTitle, data) {
        var title = removeTitle;//贸易
        if(!data) {
            return;
        }
        for(var index = 0; index < data.length; index++) {
            if(data[index].data && data[index].data.length) {
                this.removeChildMenu(slider, title, data[index].data);
            } else {
                slider = this.removeSliderMenuItem(slider, title, data[index].name);
            }
        }
        return slider;
    },
    buildCurrentPlayerSlider: function(slider, removeMenuData) {
        var removeTitle = removeMenuData.title;
        var removeData = removeMenuData.data;
        for(var index = 0; index < slider.length; index++) {
            if(removeTitle == slider[index].title && removeData.length == 0) {
                slider.splice(index--, 1);
            } else if(removeTitle == slider[index].title) {
                slider = this.removeChildMenu(slider, removeTitle, removeData);
            }
        }
        return slider;
    },
    buildSlider: function(slider, removeMenuItem, code) {
        for(var index = 0; index < removeMenuItem[code].length; index++) {
            slider = this.buildCurrentPlayerSlider(slider, removeMenuItem[code][index]);
        }
        return slider;
    },
    getCurrentPlayerSlider: function(slider, userType, primary) {
        if(primary == "false") {//如果不是主账号，去掉子账号管理
            slider = this.buildSlider(slider, removeMenuItem, "primary");
        }
        switch(userType) {
            case 1: {
                return this.buildSlider(slider, removeMenuItem, LOGISTICS);
            }
            case 2: {
                return this.buildSlider(slider, removeMenuItem, TRADE);
            }
            case 3: {//物流贸易用户返回全部菜单项
                return slider;
            }
            case 256: {
                return this.buildSlider(slider, removeMenuItem, SUPPLER_TRADE);;
            }
        }
    },
    render:function() {
        this.tree = [];
        var data = this.state.data;
        var userType = 1;
        if(data["userType"]) {
            userType = parseInt(data["userType"]);
        }
        var primary = data["primary"];
        var sliderTitle = data.title === '个人中心' ? 'slider-title personal' : 'slider-title';
        var slider = data.data;
        if(typeof primary != "undefined" && typeof data["userType"] != "undefined") {
            if (typeof data["userType"] == "string") {
                slider = this.getCurrentPlayerSlider(slider, userType, primary);
            }
            if (userType == SUPPLER_TRADE && slider.length) {
                for (var index = 0; index < slider.length; index++) {
                    if (slider[index].name == "我要下单") {
                        slider.splice(index, 1);
                        break;
                    }
                }
            }
            for (var index = 0; index < slider.length; index++) {
                if (slider[index].name == "我要下单") {
                    if (userType == LOGISTICS) {
                        slider[index].url = "/product_list";
                    } else if (userType == TRADE) {
                        slider[index].url = "/trdOrdersList";
                    }
                }
            }
        }
        var pageUrl = data["activeItem"] ? data["activeItem"] : "";
        this.pageUrl = pageUrl;
        this.userType = userType;
        var sliderItems = this.getMenu(slider);
        return ~slider;
    }
});
module.exports = sliderComponent;